package com.itboye.eweilisten.me.timu;

import android.annotation.SuppressLint;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import com.itboye.eweilisten.R;
import com.itboye.eweilisten.adapter.CuoTiJieXiAdapter;
import com.itboye.eweilisten.base.BaseFragment;
import com.itboye.eweilisten.model.TIbean;
import com.itboye.eweilisten.model.TIbeanTwo;
import com.itboye.eweilisten.model.TiXingBean;
import com.itboye.eweilisten.model.YesCuoBean;

import java.util.ArrayList;
import java.util.List;

/**
 * 错题分析
 */
@SuppressLint("ValidFragment")
public class FenXiFragment extends BaseFragment {
    RecyclerView recyclerView;

    YesCuoBean.InfoList bean;
    YesCuoBean yesCuoBean;
    TextView jiexi_shang, jiexi_xia;
    public List<TIbeanTwo> beanTi = new ArrayList<>();

    CuoTiJieXiAdapter adapter;
    ArrayList<ArrayList<String>> questions = new ArrayList<ArrayList<String>>();//内容
    ArrayList<TIbean.Options> answers = new ArrayList<TIbean.Options>();//答案前缀
    ArrayList<String> answerss = new ArrayList<String>();//答案内容
    ArrayList<String> answersHead = new ArrayList<String>();//答案内容

    ArrayList<String> answers_nei;//子类答案内容
    ArrayList<TIbean.Options> answers_qian;//子类答案前缀
    List<TIbean.Clild> cc = new ArrayList<TIbean.Clild>();

    ArrayList<String> que = new ArrayList<String>();//用来存6227类型的1.2.3.4

    public FenXiFragment(YesCuoBean.InfoList bean,YesCuoBean yesCuoBean) {
        this.bean = bean;
        this.yesCuoBean=yesCuoBean;

    }

    @Override
    public int initView() {
        return R.layout.activity_fen_xi_fragment;
    }

    @Override
    public void onMyActivityCreated() {

        read(bean);
    }

    @Override
    public void onClick(View v) {

    }

    /**
     * 数据填充
     */
    private void read(YesCuoBean.InfoList bean) {
        questions = new ArrayList<ArrayList<String>>();
        answers = new ArrayList<>();
        answerss = new ArrayList<>();
        answersHead=new ArrayList<>();
        TIbeanTwo b = new TIbeanTwo();

        int index = 0;
        if (bean.getDt_type().equals("6227")) {//注：6227类型空格填1.2.3.4 ,所以额外添加了一个不一样的
            for (int j = 0; j < bean.getContent().get(0).size(); j++) {
                if ("con".equals(bean.getContent().get(0).get(j)[0] + "")) {
                    //___
                    index++;
                    que.add(index + "_");
                } else if ("str".equals(bean.getContent().get(0).get(j)[0] + "")) {
                    //有文字
                    que.add(bean.getContent().get(0).get(j)[1] + "");
                }
            }
            questions.add(que);

        } else {
            //内容
            for (int i1 = 0; i1 < bean.getContent().size(); i1++) {
                ArrayList<String> ques = new ArrayList<String>();
                for (int j = 0; j < bean.getContent().get(i1).size(); j++) {
                    if ("con".equals(bean.getContent().get(i1).get(j)[0] + "")) {
                        if (bean.getContent().get(i1).get(j)[1].equals("nl")) {
                            ques.add("nl");
                        } else {
                            ques.add("_");
                        }
                    } else if ("str".equals(bean.getContent().get(i1).get(j)[0] + "")) {
                        //有文字
                        ques.add(bean.getContent().get(i1).get(j)[1] + "");

                    } else if ("img".equals(bean.getContent().get(i1).get(j)[0] + "")) {
                        ques.add(bean.getContent().get(i1).get(j)[1] + "");
                    }
                }
                questions.add(ques);
            }
        }

        b.setContent(questions);
        //答案前缀
        for (int j = 0; j < bean.getAnswer().size(); j++) {
            TIbean.Options op = new TIbean.Options();
            if ("str".equals(bean.getAnswer().get(j)[1] + "")) {
                op.setOptions(bean.getAnswer().get(j)[0] + "");
                op.setNeirong(bean.getAnswer().get(j)[2] + "");
                op.setDa(false);
                answers.add(op);
            }
            if ("bool".equals(bean.getAnswer().get(j)[1] + "")) {
                op.setOptions(bean.getAnswer().get(j)[0] + "");
                op.setNeirong(bean.getAnswer().get(j)[2] + "");
                op.setDa(false);
                answers.add(op);
            }
            if ("img".equals(bean.getAnswer().get(j)[1] + "")) {
                op.setOptions(bean.getAnswer().get(j)[0] + "");
                op.setNeirong(bean.getAnswer().get(j)[2] + "");
                op.setDa(false);
                answers.add(op);
            }
        }
        b.setOption(answers);
        //答案内容
        for (int j = 0; j < bean.getAnswer().size(); j++) {
            if ("str".equals(bean.getAnswer().get(j)[1] + "")) {
                answerss.add(bean.getAnswer().get(j)[2] + "");
                answersHead.add(bean.getAnswer().get(j)[0] + "");
            }
            if ("bool".equals(bean.getAnswer().get(j)[1] + "")) {
                answerss.add(bean.getAnswer().get(j)[2] + "");
                answersHead.add(bean.getAnswer().get(j)[0] + "");
            }
            if ("img".equals(bean.getAnswer().get(j)[1] + "")) {
                answerss.add(bean.getAnswer().get(j)[2] + "");
                answersHead.add(bean.getAnswer().get(j)[0] + "");
            }
        }

        //子类题目
        List<TiXingBean.ListInfo.Child> child = bean.getChild();
        for (int i1 = 0; i1 < child.size(); i1++) {
            answers_nei = new ArrayList<String>();//答案内容
            answers_qian = new ArrayList<TIbean.Options>();//答案前缀
            TIbean.Clild cl = new TIbean.Clild();
            //答案内容
            for (int j = 0; j < child.get(i1).getAnswer().size(); j++) {
                if ("str".equals(child.get(i1).getAnswer().get(j)[1] + "")) {
                    answers_nei.add(child.get(i1).getAnswer().get(j)[2] + "");
                }
            }
            cl.setAnswers(answers_nei);
            //答案前缀
            for (int j = 0; j < child.get(i1).getAnswer().size(); j++) {
                TIbean.Options op = new TIbean.Options();
                if ("str".equals(child.get(i1).getAnswer().get(j)[1] + "")) {
                    op.setOptions(child.get(i1).getAnswer().get(j)[0] + "");
                    op.setDa(false);
                    answers_qian.add(op);
                }
            }
            cl.setAnwser_true(child.get(i1).getAnswer_true());
            cl.setId(child.get(i1).getId());
            cl.setOption(answers_qian);
            cc.add(cl);
        }
        b.setClild(cc);

        b.setAnswers(answerss);
        b.setAnswerHead(answersHead);
        b.setAnwser_true(bean.getAnswer_true());
        b.setTitle(bean.getTitle());
        b.setDt_type(bean.getDt_type());
        b.setQuestion(bean.getQuestion());
        b.setAudioPath(bean.getAudio_path());
        b.setAudioDuration(bean.getAudio_duration());
        b.setOriginArticle(bean.getOrigin_article());
        beanTi.add(b);


        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        setAdater();
    }

    public void setAdater() {
//        if (adapter == null) {

            adapter = new CuoTiJieXiAdapter(getActivity(), beanTi,yesCuoBean);

            recyclerView.setAdapter(adapter);
//        } else {
//            adapter.notifyDataSetChanged();
//        }
    }
}
